#!/usr/bin/env bash

export REMOTE_USER=$1
export REMOTE_HOST=$2
export REMOTE_PORT=$3
export LOCAL_HOST=$4
export LOCAL_PORT=$5

echo "REMOTE_HOST=[${REMOTE_HOST}]"
echo "REMOTE_PORT=[${REMOTE_PORT}]"
echo "REMOTE_USER=[${REMOTE_USER}]"
echo "LOCAL_HOST=[${LOCAL_HOST}]"
echo "LOCAL_PORT=[${LOCAL_PORT}]"

if [  ! -n "${REMOTE_HOST}" ] ; then
	return 254
fi

if [  ! -n "${REMOTE_PORT}" ] ; then
	return 254
fi

if [  ! -n "${REMOTE_USER}" ] ; then
	return 254
fi

if [  ! -n "${LOCAL_HOST}" ] ; then
	return 254
fi

if [  ! -n "${LOCAL_PORT}" ] ; then
	return 254
fi

while true ; do 
	echo -n "$(date +"%Y-%m-%d %H:%M:%S")  "
	/usr/bin/ssh -gNR ${REMOTE_HOST}:${REMOTE_PORT}:${LOCAL_HOST}:${LOCAL_PORT} -o ExitOnForwardFailure=yes -o TCPKeepAlive=yes -o ServerAliveInterval=10 -o ServerAliveCountMax=1 ${REMOTE_USER}@${REMOTE_HOST}
	sleep 3
done
